//
//  LineChartView.swift
//  动态表格
//
//  Created by app on 2017/4/17.
//  Copyright © 2017年 app. All rights reserved.
//

import UIKit

let PNGColor = UIColor(red: 77/255, green: 186/255, blue: 122/255, alpha: 1.0)
class LineChartView: UIView {
    func drawLineChart(){
        chartLine.strokeEnd = 1.0
        chartLine.add(pathAnmimation, forKey: nil)
        
    }

    
    var chartLine = CAShapeLayer()
    var pathAnmimation = CABasicAnimation()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        clipsToBounds = true
        chartLine.lineCap = kCALineCapRound
        chartLine.lineJoin = kCALineJoinRound
        chartLine.fillColor = UIColor.white.cgColor
        chartLine.lineWidth = 10.0
        chartLine.strokeEnd = 0.0
        layer.addSublayer(chartLine)
    }
    
    override func draw(_ rect: CGRect) {
        let line = UIBezierPath()
        line.lineWidth = 10
        line.lineCapStyle = .round
        line.lineJoinStyle = .round
        line.move(to: CGPoint(x: 70, y: 260))
        line.addLine(to: CGPoint(x: 140, y: 100))
        line.addLine(to: CGPoint(x: 210, y: 240))
        line.addLine(to: CGPoint(x: 280, y: 170))
        line.addLine(to: CGPoint(x: 350, y: 220))
        chartLine.path = line.cgPath
        chartLine.strokeColor = PNGColor.cgColor
        
        pathAnmimation.keyPath = "strokeEnd"
        pathAnmimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        pathAnmimation.fromValue = 0.0
        pathAnmimation.toValue = 1.0
        pathAnmimation.autoreverses = false
        pathAnmimation.duration = 5.0
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
